package com.jinlia.show.modules.system.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jinlia.show.common.core.utils.PageUtils;
import com.jinlia.show.modules.base.system.entity.SysDictType;
import com.jinlia.show.modules.base.model.form.DictTypeForm;
import com.jinlia.show.modules.base.model.po.SysDictTypePO;
import com.jinlia.show.modules.base.model.query.DictTypePageQuery;
import com.jinlia.show.modules.base.system.service.SysDictTypeMapperService;
import com.jinlia.show.modules.system.model.Option;
import com.jinlia.show.modules.system.service.ifc.ISysDictTypeService;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

@Service
@RequiredArgsConstructor
public class SysDictTypeService extends SysDictTypeMapperService implements ISysDictTypeService {

    @Autowired
    private SysDictService dictItemService;
    @Override
    public Page<SysDictTypePO> getDictTypePage(DictTypePageQuery queryParams) {
        // 查询参数
        int pageNum = queryParams.getCurrent();
        int pageSize = queryParams.getSize();
        String keywords = queryParams.getKeywords();

        // 查询数据
        Page<SysDictType> dictTypePage = this.page(
                new Page<>(pageNum, pageSize),
                new LambdaQueryWrapper<SysDictType>()
                        .like(StringUtils.hasLength(keywords), SysDictType::getName, keywords)
                        .or()
                        .like(StringUtils.hasLength(keywords), SysDictType::getCode, keywords)
                        .select(
                                SysDictType::getId,
                                SysDictType::getName,
                                SysDictType::getCode,
                                SysDictType::getStatus,
                                SysDictType::getRemark
                        )
        );
        Page<SysDictTypePO> sysDictTypeVOPage = PageUtils.tToVO(dictTypePage, SysDictTypePO.class);
        return sysDictTypeVOPage;
    }

    @Override
    public SysDictTypePO getDictTypeForm(Long id) {
        // 获取entity
        SysDictType entity = this.getOne(new LambdaQueryWrapper<SysDictType>()
                .eq(SysDictType::getId, id)
                .select(
                        SysDictType::getId,
                        SysDictType::getName,
                        SysDictType::getCode,
                        SysDictType::getStatus,
                        SysDictType::getRemark
                ));
        return SysDictTypePO.fromEntity(entity);
    }

    @Override
    public boolean saveDictType(SysDictTypePO sysDictTypeVO) {
        // 持久化
        boolean result = this.save(SysDictTypePO.toEntity(sysDictTypeVO));
        return Boolean.TRUE;
    }

    @Override
    public boolean updateDictType(Long id, DictTypeForm dictTypeForm) {
        // 获取字典类型
        SysDictType sysDictType = this.getById(id);

//        SysDictType entity = dictTypeConverter.form2Entity(dictTypeForm);
//        boolean result = this.updateById(entity);
//        if (result) {
//            // 字典类型code变化，同步修改字典项的类型code
//            String oldCode = sysDictType.getCode();
//            String newCode = dictTypeForm.getCode();
//            if (!StrUtil.equals(oldCode, newCode)) {
//                dictItemService.update(new LambdaUpdateWrapper<SysDict>()
//                        .eq(SysDict::getTypeCode, oldCode)
//                        .set(SysDict::getTypeCode, newCode)
//                );
//            }
//        }
        return Boolean.TRUE;
    }

    @Override
    public boolean deleteDictTypes(String idsStr) {

        List ids = Arrays.asList(idsStr.split(","))
                .stream()
                .collect(Collectors.toList());

//        // 删除字典数据项
//        List<String> dictTypeCodes = this.list(new LambdaQueryWrapper<SysDictType>()
//                .in(SysDictType::getId, ids)
//                .select(SysDictType::getCode))
//                .stream()
//                .map(dictType -> dictType.getCode())
//                .collect(Collectors.toList()
//                );
//        if (CollectionUtil.isNotEmpty(dictTypeCodes)) {
//            dictItemService.remove(new LambdaQueryWrapper<SysDict>()
//                    .in(SysDict::getTypeCode, dictTypeCodes));
//        }
//        // 删除字典类型
//        boolean result = this.removeByIds(ids);
        return false;
    }

    @Override
    public List<Option> listDictItemsByTypeCode(String typeCode) {
        // 数据字典项
//        List<SysDict> dictItems = dictItemService.list(new LambdaQueryWrapper<SysDict>()
//                .eq(SysDict::getTypeCode, typeCode)
//                .select(SysDict::getValue, SysDict::getName)
//        );
//
//        // 转换下拉数据
//        List<Option> options = CollectionUtil.emptyIfNull(dictItems)
//                .stream()
//                .map(dictItem -> new Option(dictItem.getValue(), dictItem.getName()))
//                .collect(Collectors.toList());
        return null;
    }
}
